package com.eifesun.monitor.application;

import com.eifesun.monitor.DeviceType;

public class ModbusClient {

    private String clientId;
    private DeviceType deviceType;
    private boolean active;
    private int startAddress;
    private int quantity;
    private int slaveId;
    private int functionCode;
    private CommunicationType communicationType;

    public static class Builder {
        private String clientId = "";
        private DeviceType deviceType = DeviceType.INVERTER;
        private boolean active = true;
        private int startAddress = 0;
        private int quantity = 0;
        private int slaveId = 0;
        private int functionCode = 4;
        private CommunicationType communicationType = CommunicationType.TCP_SERVER;

        public Builder() {
        }

        public Builder configBasic(String clientIdVal, String deviceTypeVal, boolean activeVal) {
            clientId = clientIdVal;
            deviceType = DeviceType.fromValue(deviceTypeVal);
            active = activeVal;
            return this;
        }

        public Builder configRequest(int startAddressVal, int quantityVal, int slaveIdVal) {
            startAddress = startAddressVal;
            quantity = quantityVal;
            slaveId = slaveIdVal;
            return this;
        }

        public Builder configFunctionCode(int functioncodeVal) {
            functionCode = functioncodeVal;
            return this;
        }

        public Builder configCommunicationType(String communicationTypeVal) {
            communicationType = CommunicationType.valueOf(communicationTypeVal);
            return this;
        }

        public ModbusClient build() {
            return new ModbusClient(this);
        }
    }

    public ModbusClient(Builder builder) {

        clientId = builder.clientId;
        deviceType = builder.deviceType;
        active = builder.active;
        startAddress = builder.startAddress;
        quantity = builder.quantity;
        slaveId = builder.slaveId;
        functionCode = builder.functionCode;
        communicationType = builder.communicationType;
    }

    private enum CommunicationType {
        TCP_CLIENT, TCP_SERVER, SERIAL_CLIENT;
    }

    public int getStartAddress() {
        return startAddress;
    }

    public int getQuantity() {
        return quantity;
    }

    public int getSlaveId() {
        return slaveId;
    }

    public String getClientId() {
        return clientId;
    }

    public DeviceType getDeviceType() {
        return deviceType;
    }

    public boolean isActive() {
        return active;
    }

    public int getFunctionCode() {
        return functionCode;
    }

    public boolean isTcpConnection() {

        return !(communicationType == CommunicationType.SERIAL_CLIENT);
    }

    @Override
    public String toString() {
        return "ModbusClient [clientId=" + clientId + ", deviceType=" + deviceType + ", startAddress=" + startAddress + ", quantity=" + quantity +
                ", slaveId=" + slaveId + ", functionCode=" + functionCode + ", communicationType=" + communicationType + "]";
    }

}
